package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.model.bo.StorageBo;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.ImageVo;
import com.cskaoyan.catmall.model.vo.StorageVo;
import com.cskaoyan.catmall.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @description
 * @author czx
 * @date 2022-01-10 17:20
 * @param
 * @param null
 * @return
 */
@RestController
@RequestMapping("admin/storage")
public class StorageController {

    @Autowired
    StorageService storageService;


    @GetMapping("list")
    public BaseRespVo list(StorageBo storageBo){

        StorageVo storageVo = storageService.selectStorage(storageBo);
        return BaseRespVo.ok(storageVo);

    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody StorageVo.ListDTO listDTO){
        int res = storageService.deleteStorage(listDTO);

        if(res == 1){
           return BaseRespVo.ok();
        }else{
            return BaseRespVo.fail();
        }

    }

    @PostMapping("create")
    public BaseRespVo create(MultipartFile file){
        System.out.println(file);
        ImageVo imageVo = storageService.uploadFile(file);
        return BaseRespVo.ok(imageVo);
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody StorageVo.ListDTO listDTO){
        int res = storageService.updateFileName(listDTO);

        if(res == 1){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail();
        }
    }

}
